package com.xxpt.user.feign;

import com.xxpt.model.Result;
import com.xxpt.model.user.User;
import com.xxpt.user.dto.AddUserDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;

@FeignClient(name = "user", contextId = "account", fallbackFactory = AccountFeign.AccountFallbackFactory.class)
public interface AccountFeign {
    @RequestMapping(method = RequestMethod.POST, value = "/user/add", consumes = "application/json")
    Result<User> add(@RequestBody AddUserDTO userDTO);

    @RequestMapping(method = RequestMethod.GET, value = "/user/findById")
    User findById(@RequestParam("id") Long id);

    @Slf4j
    class AccountFallbackFactory implements FallbackFactory<AccountFeign> {
        @Override
        public AccountFeign create(Throwable cause) {
            log.error(cause.getMessage());
            return new AccountFeign() {
                @Override
                public Result<User> add(AddUserDTO userDTO) {
                    return null;
                }

                @Override
                public User findById(Long id) {
                    return null;
                }
            };
        }
    }
}
